<?php

namespace App\Models;


use App\Service\Base\Model;

class SiteConfig extends Model
{

    // 系统配置
    protected $table = 'site_config';

    // 缓存字段，用于搜索绑定的字段
    protected $cacheFields = ['id', 'field'];

    // 保存
    public function onUpdate($data)
    {
        foreach($data as $key => $item){
            /*$info = $this->where('field', $item['field'])->first();
            if(empty($info)){
                $result = $this->where('id', $info->id)->update(['value' => $item['value']]);
            } else{
                $result = $this->insert(['value' => $item['value'], 'field' => $item['field']]);
            }*/
        }

        return true;
    }

    // 获取配置信息
    public function getConfig()
    {
        // 获取缓存
        $bang     = $this->bang;
        $is_cache = $this->is_cache || $bang;
        $args     = [];
        if($is_cache){
            $cacheName = $this->cacheKey('lists', $args);
            $lists     = $this->cacheClass()->getCache($cacheName);
            // 检查是否已经绑定
            $this->checkBangList($lists, $cacheName);
        }
        if(empty($lists)){
            $lists = $this->get()->toArray();
            // 保存为缓存
            if($is_cache){
                $this->addCacheList($lists, $cacheName, $lists, 'list');
            }
        }

        // 整理
        $config = array_column($lists, null, 'field');
        /*$config = [];
        foreach($lists as $_row){
            $config[$_row['field']] = $_row;
        }*/

        return $config;
    }


    // 定义验证操作
    public function rules()
    {

        $add = [
        ];

        $edit = [
        ];

        $info = [
        ];


        $rule = [
            //'id'          => 'required',
            'name'  => 'required',
            'field' => 'required',
            //'en_value'    => 'required',
            //'cn_value'    => 'required',
            //'tc_value'    => 'required',
            //'type'        => 'required',
            //'description' => 'required',
            //'extend'      => 'required',
            //'ctime'       => 'required',
            //'mtime'       => 'required',
        ];


        $msg = [
            'id'          => '',
            'name'        => '中文名',
            'field'       => '英文名',
            'en_value'    => '配置信息（英文）',
            'cn_value'    => '配置信息（中文）',
            'tc_value'    => '配置信息（繁体）',
            'type'        => '表单类型',
            'description' => '描述',
            'extend'      => '扩展',
            'ctime'       => '创建时间',
            'mtime'       => '修改时间',
        ];

        return ['rule' => $rule, 'msg' => $msg, 'add' => $add];
    }
}
